package io.polly.ldz.reactor.util;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
/**
 * @Author ldz112@163.com
 * @Classname CacheUtil
 * @Version V1.0
 * @Description 基于guavaCache实现
 * @Date 2024/9/29 17:20
 * Copyright (C) 2023 All Rights Reserved
 */
public class CacheUtil {

    static Cache<String, Object> guavaCache = CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(2, TimeUnit.HOURS).build();


    public static void put(String key,Object object){
        guavaCache.put(key,object);
    }

    public static Object get(String key) throws ExecutionException {
        Object value = guavaCache.get(key, () -> {
                String v = "v";
                guavaCache.put(key, v);
                return v;
            });
        return value;
    }

    public static Boolean isContains(String key) throws Exception {
        return get(key) == null;
    }


}
